package com.changgou.canal;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.content.feign.ContentFeign;
import com.changgou.util.RedisAPI;

import com.changgou.content.pojo.Content;
import com.changgou.entity.Result;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.DeleteListenPoint;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.UpdateListenPoint;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;


import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO 监听数据库的数据
 * @ClassName CanalDataEventListener
 * @Author YPK
 * @Date 23:31 2019/11/14
 * @Version 2.1
 **/
@Slf4j
@CanalEventListener
public class CanalDataEventListener {
    @Autowired
    private ContentFeign contentFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;//k v 字符串
    @Autowired
    private RedisTemplate redisTemplate;//k -v 对象

    /**
     * TODO
     *
     * @param: [entryType, rowData]  TODO [  监控对数据库操作的事件; 操作的行数据]
     * @return: void
     * @Author: YPK
     * @Date: 2019/11/14  23:33
     * @Version: 1.0
     **/
    @InsertListenPoint
    public void onEnventInsert(CanalEntry.EntryType entryType, CanalEntry.RowData rowData) {
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            String columnName = column.getName();   // 列名称
            String value = column.getValue();       // 列对应的值
            System.out.println("列名：" + columnName + "列值：" + value);
        }
        synRedis();
    }


    /**
     * TODO
     *
     * @param: [entryType, rowData]  TODO [  监控对数据库操作的事件; 操作的行数据]
     * @return: void
     * @Author: YPK
     * @Date: 2019/11/15  0:09
     * @Version: 1.0
     **/
    @UpdateListenPoint
    public void onEnventUpdate(CanalEntry.EntryType entryType, CanalEntry.RowData rowData) {
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList(); // 更新前的数据
        for (CanalEntry.Column column : beforeColumnsList) {
            String columnName = column.getName();   // 列名称
            String value = column.getValue();       // 列对应的值
            System.out.println("列名：" + columnName + "列值：" + value);
        }
        System.out.println("------------更新前后的数据--------------");
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();   // 更新后的数据
        for (CanalEntry.Column column : afterColumnsList) {
            String columnName = column.getName();   // 列名称
            String value = column.getValue();       // 列对应的值
            System.out.println("列名：" + columnName + "列值：" + value);
        }
        synRedis();
    }

    /**
     * TODO
     *
     * @param: [entryType, rowData]  TODO [  监控对数据库操作的事件; 操作的行数据]
     * @return: void
     * @Author: YPK
     * @Date: 2019/11/14  23:49
     * @Version: 1.0
     **/
    @DeleteListenPoint
    public void onEnventDelete(CanalEntry.EntryType entryType, CanalEntry.RowData rowData) {
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList(); // 更新前的数据
        for (CanalEntry.Column column : beforeColumnsList) {
            String columnName = column.getName();   // 列名称
            String value = column.getValue();       // 列对应的值
            System.out.println("列名：" + columnName + "列值：" + value);
        }
        synRedis();
    }

    private void synRedis() {
        Result<List<Content>> all = contentFeign.findAll();
        List<Content> listContent = all.getData();
        for (Content content : listContent) {
            stringRedisTemplate.opsForValue().append("content_" + content.getId(), JSON.toJSONString(content));
            stringRedisTemplate.expire("content_" + content.getId(), 10 * 60, TimeUnit.SECONDS);
           log .info("ok=======================");
        }
    }
}
